import { defineStore } from 'pinia'
import stores from '../../stores'
import { bindUserSn, ifUserSn, getAccount } from '@/services/api'
import { tnNavPage } from '@tuniao/tnui-vue3-uniapp/utils'

export const useUserStore = defineStore({
  id: 'app-user',
  state: () => ({
    ifBindSn: null,
    snDetail: {},
    currentAgentId: '',
    currentCgPage: 1,
    currentDrPage: 1,
    avatarUrl: '',
    userInfoObj: {},
    userNickName: '',
    // 标记是否第一次通过扫码绑定
    isFirstBind: false,
  }),
  getters: {
    // 获取 用户信息
    getUserInfoObj(state) {
      return state.userInfoObj
    },
    // 获取是否执行扫码绑定操作并成功
    getIsFirstBind(state) {
      return state.isFirstBind
    },
    // 获取头像
    getAvatarUrl(state) {
      return state.avatarUrl
    },
    // 获取用户名
    getUserNickName(state) {
      return state.userNickName
    },
    getIfBindSn(state) {
      return state.ifBindSn
    },
    getSnDetail(state) {
      return state.snDetail
    },
    getCurrentDrPage(state) {
      return state.currentDrPage
    },
  },
  actions: {
    cutCurrentCgPage(data) {
      if (data === 1) {
        this.currentCgPage = 1
      } else {
        this.currentCgPage++
      }
    },
    cutCurrentDrPage(data) {
      if (data === 1) {
        this.currentDrPage = 1
      } else {
        this.currentDrPage++
      }
    },
    //查询用户是否绑定过设备SN和更新绑定信息
    async checkUserSn() {
      const res = await ifUserSn()
      if (res.code === 0) {
        this.ifBindSn = res.data.Binding == 1 ? true : false

        this.snDetail = res.data
      }
      console.log(res)
    },
    // 用户绑定设备SN
    async toBindUserSn(snId: string) {
      const res = await bindUserSn(snId)
      if (res.code === 0) {
        //查看和更新绑定信息
        await this.checkUserSn()
        // 扫码绑定后
        this.isFirstBind = true
        // 根据需求跳转至首页选择年级
        tnNavPage('/pages/typeStudy/dialogue/index', 'switchTab')
        uni.showToast({
          title: '绑定成功',
          duration: 2000,
          icon: 'success',
        })
        console.log('绑定成功', res)
        //绑定成功
        return true
      }
      if (res.code === 1000) {
        uni.showToast({
          title: res.msg,
          duration: 2000,
          icon: 'fail',
        })
        console.log('绑定失败', res)
        return false
      }
    },
    //调起微信小程序扫码
    toScanMate() {
      uni.scanCode({
        onlyFromCamera: true, // 默认false，true为只允许从相机扫码
        success: async (res) => {
          console.info(res) //打印出来，查看自己想要的返回结果数据
          // 拿SN码去绑定设备
          await this.toBindUserSn(res.result)
        },
      })
    },
    changeCurrentAgentId(id: Number) {
      this.currentAgentId = id
    },
    //获取用户信息
    async getUserInfo() {
      getAccount().then((res) => {
        const { data, status } = res
        if (status == 'ok') {
          this.userInfoObj = res
          if (data.avatar) {
            this.avatarUrl = data.avatar
          }
          if (data.displayName) {
            this.userNickName = data.displayName
          }
        }
      })
    },
  },
})

// Need to be used outside the setup
export function useUserStoreWithOut() {
  return useUserStore(stores)
}
